package com.nj.dms.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.nj.dms.bean.FileBean;
import com.nj.dms.dao.FileDao;
import com.nj.dms.dao.impl.FileDaoImpl;
import com.nj.dms.util.FileType;

/**
 * 上传文件
 */
@WebServlet("/UploadFile")
@MultipartConfig
public class UploadFile extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 获取文件part对象
		Part part = request.getPart("file");

		// 获取用户名
		String userName = (String) request.getSession().getAttribute("userName");

		// 获取文件名称
		String fileName = part.getSubmittedFileName();

		// 获取文件类型
		int type = FileType.getFileType(fileName);

		String[] typeArr = { "empty", "others", "images", "documents", "videos", "musics" };

		// 获取版本
		String version = request.getParameter("version");

		// 获取密级
		int thick = new Integer(request.getParameter("thick"));

		// 设置审核状态
		int audit = thick == 1 ? 1 : 0;

		// 获取描述
		String description = request.getParameter("description");

		// 检查数据合法性
		boolean flag = true;// 默认数据合法
		// 检查用户名
		if (userName == null || userName.trim().length() == 0) {
			flag = false;
		}

		// 判断文件名是否为空
		if (fileName == null || fileName.trim().length() == 0) {
			// 文件未选择
			flag = false;
			request.setAttribute("fileErr", "文件未选择");
		}

		if (flag) {

			Date date = new Date();

			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

			String sPath = typeArr[type] + File.separator + userName + File.separator + sdf.format(date)
					+ File.separator + fileName;

			// 获取上传文件的地址
			String path = request.getServletContext().getRealPath("/") + "uploads" + File.separator + sPath;

			File file = new File(path);

			if (!file.getParentFile().exists()) {

				// 创建文件夹
				file.getParentFile().mkdirs();

			}

			// 上传文件
			part.write(path);

			// 将数据保存到数据库
			FileBean uFile = new FileBean(userName, fileName, description, sPath, type, thick, audit, version, date,
					date);

			FileDao fileDao = new FileDaoImpl();

			if (fileDao.addFile(uFile)) {

				// 保存数据成功，跳转到增加文档的目录
				response.sendRedirect("document/add-document-menu.jsp");

			} else {
				// 保存数据失败，跳转到上传文件的表单

				// 保存数据
				request.setAttribute("version", version);
				request.setAttribute("thick", thick);
				request.setAttribute("description", description);

				// 执行服务器端跳转，不改变浏览器的地址
				request.getRequestDispatcher("document/upload-file.jsp").forward(request, response);

			}

		} else {
			// 保存数据失败，跳转到上传文件的表单

			// 保存数据
			request.setAttribute("version", version);
			request.setAttribute("thick", thick);
			request.setAttribute("description", description);

			// 执行服务器端跳转，不改变浏览器的地址
			request.getRequestDispatcher("document/upload-file.jsp").forward(request, response);

		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
